package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.part.DeletePartBo;
import com.zhuss.bean.vo.core.part.DetailPartVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.PartDao;
import com.zhuss.core.db.entity.Part;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 删除区域 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("partDelete")
public class PartDelete extends AbstractProcessor<Object, DeletePartBo> implements Processor<Object, DeletePartBo> {

    @Autowired
    private PartDao partDao;
    
    @Autowired
	@Qualifier("partRead")
	private PartRead partRead;
    
    
    @Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
    
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    
    @Autowired
	@Qualifier("roleRead")
	private RoleRead roleRead;
    
    
    /**
     * 校验
     */
	@Override
	@SuppressWarnings("unchecked")
    public void check(DeletePartBo bo) throws WpsException {
    	Long partId = bo.getPartId();
    	DetailPartVo part = (DetailPartVo) partRead.detail(partId);
    	Integer area = part.getArea();
    	List<Long> companyIdList = (List<Long>) companyRead.listCompanyId(area);
    	if (!companyIdList.isEmpty()) {
    		throw new WpsException(ExceptionCode.不能删除有公司的区域);
    	}
    	List<Long> userIdList = (List<Long>) userRead.listUserId(area);
    	if (!userIdList.isEmpty()) {
    		throw new WpsException(ExceptionCode.不能删除有用户的区域);
    	}
    	List<Long> roleIdList = (List<Long>) roleRead.listRole(area);
    	if (!roleIdList.isEmpty()) {
    		throw new WpsException(ExceptionCode.不能删除有角色的区域);
    	}
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeletePartBo param) throws WpsException {
    	Long partId = param.getPartId();
    	String name = ((DetailPartVo) partRead.detail(partId)).getArea() + "";
    	
    	Part record = new Part();
    	record.setPartId(partId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	partDao.delete(record);
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeletePartBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.区域删除.key());
    	record.setTargetId(param.getPartId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
